package com.example.project.Function.register;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.project.Function.code.code;
import com.example.project.R;
import com.example.project.key.Dao.employeeDao.employeeDao;
import com.example.project.key.connection.databaseconn;
import com.example.project.key.service.employeeService.employeeService;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class register extends AppCompatActivity {
    private Button btn_reg;
    private EditText ed_username;
    private EditText ed_pwd;
    private EditText ed_email;
    private EditText ed_gender;
    private EditText ed_age;
    private EditText telenum;
    private EditText dep;
    private EditText id;
    private EditText code1;
    private EditText worknum;

    private EditText mEtloginactivityPhonecodes;
    private ImageView mIvloginactivityShowcode;
    private String realCode;

    private static Connection con=null;
    private static PreparedStatement stmt = null;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);



        btn_reg=findViewById(R.id.finish_reg);
        ed_username=findViewById(R.id.ed_username);
        ed_pwd=findViewById(R.id.ed_regpassword);
        ed_age=findViewById(R.id.ed_age);
        ed_email=findViewById(R.id.ed_emgp);
        ed_gender=findViewById(R.id.ed_infosex);
        id=findViewById(R.id.ed_idcard);
        telenum=findViewById(R.id.ed_telnum);
        code1 =findViewById(R.id.ed_code);
        dep=findViewById(R.id.ed_dep);
        worknum=findViewById(R.id.ed_worknum);

        Pattern p =Pattern.compile("[0-9]*");

        try {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    con = databaseconn.getConnection();
                }
            }).start();
        }catch (Exception e)
        {
            e.printStackTrace();
        }

        mEtloginactivityPhonecodes = findViewById(R.id.ed_code);
        mIvloginactivityShowcode = findViewById(R.id.img_code);
        mIvloginactivityShowcode.setImageBitmap(code.getInstance().createBitmap());
        realCode = code.getInstance().getCode().toLowerCase(); //将验证码用图片的形式显示出来
        mIvloginactivityShowcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mIvloginactivityShowcode.setImageBitmap(code.getInstance().createBitmap());
                realCode = code.getInstance().getCode().toLowerCase(); //将验证码用图片的形式显示出来
            }
        });

        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(id.getText().toString().isEmpty()||ed_age.getText().toString().isEmpty()||dep.getText().toString().isEmpty()||
                ed_email.getText().toString().isEmpty()||ed_pwd.getText().toString().isEmpty()||telenum.getText().toString().isEmpty()||
                code1.getText().toString().isEmpty()||ed_gender.getText().toString().isEmpty()||ed_username.getText().toString().isEmpty()
                ||worknum.getText().toString().isEmpty()){
                    Toast.makeText(register.this,"注册信息不完整",Toast.LENGTH_LONG).show();
                }else {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            employeeService ems=new employeeService();
                            if(ems.isExist(con,id.getText().toString(),ed_username.getText().toString())){
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(register.this,"身份证号码或名字已存在",Toast.LENGTH_LONG).show();
                                    }
                                });
                            }else {
                                String phoneCode = mEtloginactivityPhonecodes.getText().toString().toLowerCase();
                                if(phoneCode.length()==0){
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(register.this,"验证码不能为空",Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }
                                else if (!phoneCode.equals(realCode)){
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(register.this,"验证码错误",Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }else{
                                    Matcher m=p.matcher(worknum.getText().toString());
                                    if(m.matches()){
                                        if(ems.add(con,dep.getText().toString(),worknum.getText().toString(),id.getText().toString(),telenum.getText().toString(),
                                                ed_gender.getText().toString(),ed_email.getText().toString(),ed_age.getText().toString()
                                                ,ed_username.getText().toString(),ed_pwd.getText().toString()))
                                        {
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    Toast.makeText(register.this,"注册成功",Toast.LENGTH_LONG).show();
                                                }
                                            });
                                        }else {
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    Toast.makeText(register.this,"注册失败",Toast.LENGTH_LONG).show();
                                                }
                                            });
                                        }
                                    }else {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Toast.makeText(register.this,"注册失败,工号必须为纯数字",Toast.LENGTH_LONG).show();
                                            }
                                        });
                                    }

                                }
                            }
                        }
                    }).start();

                }

            }
        });
    }
}
